package cn.edu.ujn.finaltest.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mysql.jdbc.StringUtils;

import cn.edu.ujn.finaltest.dao.Product;
import cn.edu.ujn.finaltest.dao.User;
import cn.edu.ujn.finaltest.dao.UserMapper;
import cn.edu.ujn.finaltest.util.Page;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
	
	@Autowired
	private UserMapper userDao;
	
	@Override
	public int deleteByPrimaryKey(Integer userId) {
		// TODO Auto-generated method stub
		int deleteByPrimaryKey = this.userDao.deleteByPrimaryKey(userId);
		return deleteByPrimaryKey;
	}

	@Override
	public int insert(User row) {
		// TODO Auto-generated method stub
		int insert = this.userDao.insert(row);
		return insert;
	}

	@Override
	public int insertSelective(User row) {
		// TODO Auto-generated method stub
		int insertSelective = this.userDao.insertSelective(row);
		return insertSelective;
	}

	@Override
	public User selectByPrimaryKey(Integer userId) {
		// TODO Auto-generated method stub
		User user = this.userDao.selectByPrimaryKey(userId);
		return user;
	}

	@Override
	public int updateByPrimaryKeySelective(User row) {
		// TODO Auto-generated method stub
		int updateByPrimaryKeySelective = this.userDao.updateByPrimaryKeySelective(row);
		return updateByPrimaryKeySelective;
	}

	@Override
	public int updateByPrimaryKey(User row) {
		// TODO Auto-generated method stub
		int updateByPrimaryKey = this.userDao.updateByPrimaryKey(row);
		return updateByPrimaryKey;
	}

	@Override
	public User selectByCodePwd(String usercode, String password) {
		// TODO Auto-generated method stub
		User user = this.userDao.selectByCodePwd(usercode, password);
		return user;
	}
	@Override
	public Page<User> findUserList(Integer page, Integer rows, Integer user_id, String user_code,String user_name,String user_password,Integer user_state) {
		// TODO Auto-generated method stub

		User user = new User();
		user.setUser_id(user_id);
		user.setUser_state(user_state);
		if(!StringUtils.isNullOrEmpty(user_code)) {
			user.setUser_code(user_code);
		}
		if(!StringUtils.isNullOrEmpty(user_name)) {
			user.setUser_name(user_name);	}
		if(!StringUtils.isNullOrEmpty(user_password)) {
			user.setUser_password(user_password);
		}
		user.setStart((page-1) * rows);
		user.setRows(rows);
		List<User> users = this.userDao.findAll(user);
		//查询用户列表总记录数
		Integer count = this.userDao.selectUserListCount(user);
		Page<User> result = new Page<User>();
		result.setPage(page);
		result.setRows(users);
		result.setSize(rows);
		result.setTotal(count);
		return result;
		
	}
	
	
	@Override
	public Integer selectUserListCount(User user) {
		// TODO Auto-generated method stub
		return this.userDao.selectUserListCount(user);
	}
	
	@Override
	public List<User> findAll(User user){
		return this.userDao.findAll(user);
	}
}
